package com.hiof.moraa.server.controller;

import com.hiof.moraa.server.service.*;
import com.hiof.moraa.server.service.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Bergy
 */
@Controller
public class ProfileController {

    @Autowired
    MediaService mediaService;
    @Autowired
    UserService userService;
    @Autowired
    FeedService feedService;
    @Autowired
    LocationService locationService;
    @Autowired
    RouteService routeService;
    @Autowired
    MapService mapService;
    @Autowired
    RouteGeographyService routeGeographyService;
    @Autowired
    AthleteHealthFeedService athleteHealthFeedService;

    public void setFeedService(FeedService feedService) {
        this.feedService = feedService;
    }

    public void setMediaService(MediaService mediaService) {
        this.mediaService = mediaService;
    }

    public void setLocationService(LocationService locationService) {
        this.locationService = locationService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping({"/profile"})
    public String handleRequest(
            @RequestParam String username,
            @RequestParam String typeOfBrowse,
            @RequestParam int routeId,
            Map<String, Object> model,
            HttpServletRequest request) {

        String viewName = "";

        UserInfo userInfo = userService.getUserInfoByUsernameAndRouteId(username, routeId);
        model.put("userInfo", userInfo);
        model.put("feedList", feedService.getLatestFeedsOfOneRoute(routeId));
        model.put("routeId", routeId);

        if (userInfo.getTypeOfAttendant() == TypeOfAttendant.ATHLETE) {
            List<HealthFeed> healthFeedList = athleteHealthFeedService.getHeartRateHistory(userInfo.getAttendantId());
            if (healthFeedList.size() > 0) {
                model.put("healthFeedHistory", healthFeedList);
            }
        }

        String serverName = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        model.put("serverName", serverName);
        if (typeOfBrowse.equals("feed")) {
            model.put("individualFeeds", feedService.getLatestFeedsOfAthleteInOneRouteByUsername(username, routeId));
            viewName = "profileFeed";
        } else if (typeOfBrowse.equals("media")) {
            model.put("mediaList", mediaService.getAllMediaOfOneAthleteByUsernameAndRouteId(username, routeId));
            viewName = "profileMedia";
        } else if (typeOfBrowse.equals("map")) {
            LiveLocationFeed athleteLocation = locationService.getLatestLocationOfAthlete(username, routeId);

            model.put("athleteLocation", athleteLocation);
            model.put("routeMapPath", routeService.getMapPublicPath(routeId));
            model.put("mediaList", mediaService.getAllMediaOfOneAthleteByUsernameAndRouteId(username, routeId));
            try {
                List<MapPoint> mapPoints = mapService.getTruncatedGeoPointsInitializeIfNotExist(routeId, request.getSession().getServletContext());
                MapPoint athleteLocationInProfileMap = mapPoints.get(routeGeographyService.findIndexOfClosestPointOnTheRoute(athleteLocation.getGoogleGeoPoint(), mapPoints));
                model.put("mapPoints", mapPoints);
                model.put("athleteLocationInProfileMap", athleteLocationInProfileMap);

            } catch (Exception ex) {

            }
            viewName = "profileMap";
        } else {
            model.put("individualFeed", feedService.getLatestFeedsOfAthleteInOneRouteByUsername(username, routeId));
            viewName = "profileFeed";
        }
        return viewName;
    }
}
